package com.jf.demo;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.sys.android.util.DialogFactory;
import com.sys.android.util.FinalUntil;

import android.app.Activity;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

@ContentView(R.layout.register)
public class RegisterUser extends Activity {
	@ViewInject(R.id.editText1)
	private EditText edit1;
	@ViewInject(R.id.personPhone)
	private EditText personPhone;

	private SharedPreferences useShare;

	private Editor editor;

	private NotificationManager mNotificationManager;

	private String httpResult;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		ViewUtils.inject(this);// xml视图加载的处理

		mNotificationManager = (NotificationManager) this
				.getSystemService(Service.NOTIFICATION_SERVICE);

		TelephonyManager tm = (TelephonyManager) this
				.getSystemService(Context.TELEPHONY_SERVICE);
		String tel = tm.getLine1Number();
		if (tel != null) {
			tel = tel.substring(3, tel.length());

			edit1.setText(tel);
		}
	}

	/*
	 * 点击后进入密码设置页
	 */
	public void nextActivity(View view) {
		if (personPhone.getText().toString().equals(httpResult)) {
			Intent intent = new Intent();
			useShare = getSharedPreferences("userInfo", MODE_PRIVATE);
			editor = useShare.edit();
			editor.putString("username", edit1.getText().toString());
			editor.commit();

			intent.setClass(RegisterUser.this, RegisterUserNext.class);
			startActivity(intent);
			this.finish();
		} else {
			Toast.makeText(getApplicationContext(), "验证码错误", 0).show();
		}
	}

	public void backActivity(View view) {
		this.finish();
	}

	@OnClick(R.id.button1)
	public void scodeOnclick(View v) {
		// 向服务器提交参数值
		RequestParams params = new RequestParams();

		params.addQueryStringParameter("phoneNum", edit1.getText().toString());

		HttpUtils http = new HttpUtils();
		final Dialog dialog = DialogFactory.creatRequestDialog(
				RegisterUser.this, "验证码获取中……");
		dialog.show();

		http.send(HttpMethod.POST, FinalUntil.ServerAddress + "verifCode",
				params, new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						Log.i("RegisterUser", arg1
								+ FinalUntil.ServerAddress+"verifCode");
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// arg0.result返回服务器结果
						setNotiType(1, arg0.result);
						httpResult = arg0.result;
						Log.i("RegisterUser", arg0.result);
						dialog.hide();
					}
				});
	}

	/**
	 * 通知栏消息
	 * 
	 * @param iconId
	 * @param s
	 */
	protected void setNotiType(int iconId, String s) {
		Intent intent = new Intent();
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		PendingIntent appIntent = PendingIntent.getActivity(this, 0, intent, 0);
		Notification myNoti = new Notification();
		myNoti.icon = iconId;
		myNoti.tickerText = s;
		myNoti.defaults = Notification.DEFAULT_SOUND;
		myNoti.flags |= Notification.FLAG_AUTO_CANCEL;
		myNoti.setLatestEventInfo(this, "消息", s, appIntent);
		mNotificationManager.notify(0, myNoti);
	}
}
